package com.yxzx.ebs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzx.common.EduException;
import com.yxzx.common.ResultCode;
import com.yxzx.ebs.entity.EbsCourse;
import com.yxzx.ebs.entity.EbsCourseDescription;
import com.yxzx.ebs.entity.vo.CourseInfoForm;
import com.yxzx.ebs.entity.vo.CoursePublishVo;
import com.yxzx.ebs.entity.vo.CourseQueryVo;
import com.yxzx.ebs.entity.vo.EbsCourseWebVo;
import com.yxzx.ebs.mapper.EbsCourseMapper;
import com.yxzx.ebs.service.EbsChapterService;
import com.yxzx.ebs.service.EbsCourseDescriptionService;
import com.yxzx.ebs.service.EbsCourseService;
import com.yxzx.ebs.service.EbsVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author kongfuzi
 * @since 2020-07-30
 */
@Service
public class EbsCourseServiceImpl extends ServiceImpl<EbsCourseMapper, EbsCourse> implements EbsCourseService {

    @Autowired
    private EbsCourseDescriptionService courseDescriptionService;
    @Autowired
    private EbsVideoService videoService;
    @Autowired
    private EbsChapterService chapterService;
    /**
     * 添加课程信息
     * @param courseInfoForm
     * @return
     */
    @Override
    @Transactional
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        //1.添加课程的基本信息到课程表
        EbsCourse ebsCourse = new EbsCourse();
        BeanUtils.copyProperties(courseInfoForm,ebsCourse);
        int result = baseMapper.insert(ebsCourse);
        //2.课程描述的信息添加的课程描述表
        if (result > 0){
            EbsCourseDescription description = new EbsCourseDescription();
            description.setDescription(courseInfoForm.getDescription());
            description.setId(ebsCourse.getId());
            boolean b = courseDescriptionService.save(description);
            return b ? ebsCourse.getId() : null;
        }else {
            throw new EduException(ResultCode.ERROR,"添加课程基本信息出错");
        }
    }

    /**
     * 查询课程基本信息和描述信息，两张表
     * @param id
     * @return
     */
    @Cacheable(key = "'courseInfo'",value = "ebs")
    @Override
    public CourseInfoForm getCourseInfoById(String id) {
        //1.查询课程基本信息
        EbsCourse ebsCourse = baseMapper.selectById(id);
        if (ebsCourse == null){
            throw new EduException(ResultCode.ERROR,"没有课程信息");
        }
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(ebsCourse,courseInfoForm);
        //2.查询课程描述信息
        EbsCourseDescription courseDescription = courseDescriptionService.getById(id);
        if (courseDescription != null){
            courseInfoForm.setDescription(courseDescription.getDescription());
        }
        return courseInfoForm;
    }

    /**
     * 分页多条件查询课程列表
     * @param page
     * @param courseQueryVo
     */
    @Override
    public void getCourseInfoByPage(Page<EbsCourse> page, CourseQueryVo courseQueryVo) {
        QueryWrapper<EbsCourse> queryWrapper = new QueryWrapper<>();
        //根据添加时间排序
        queryWrapper.orderByDesc("gmt_create");

        if (courseQueryVo == null){
            baseMapper.selectPage(page,queryWrapper);
            return;
        }
        String title = courseQueryVo.getTitle();
        String subjectId = courseQueryVo.getSubjectId();
        String subjectParentId = courseQueryVo.getSubjectParentId();
        String teacherId = courseQueryVo.getTeacherId();

        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("teacher_id",teacherId);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id",subjectId);
        }
        baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 更加课程ID删除课程信息
     * @param id
     * @return
     */
    @Override
    public boolean removeCourseById(String id) {
        //1.删除vedio信息
        videoService.removeByCourseId(id);
        //2.删除章节信息
        chapterService.removeByCourseId(id);
        //3.删除描述信息
        courseDescriptionService.removeByCourseId(id);
        //4.删除课程信息
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 根据课程ID查询课程详情
     * @param id
     * @return
     */
    @Override
    public CoursePublishVo selectCoursePublishVoById(String id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    @Transactional
    @Override
    public EbsCourseWebVo selectInfoWebById(String courseId) {
        this.updatePageViewCount(courseId);
        return baseMapper.selectInfoWebById(courseId);
    }

    @Override
    public void updatePageViewCount(String id) {
        EbsCourse course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);
    }
}
